package cn.autumnorange.app.common.consumer.session;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.session.web.http.CookieHttpSessionIdResolver;
import org.springframework.session.web.http.CookieSerializer;
import org.springframework.session.web.http.DefaultCookieSerializer;
import org.springframework.session.web.http.HttpSessionIdResolver;

@Configuration
public class SpringSessionBeanConfiguration
//        extends SpringHttpSessionConfiguration
//    implements ImportAware
{

  @Value("${redisSessionCookieName}")
  private String redisSessionCookieName;
  //          = "CLIENTSESSIONID";

  //  private Integer maxInactiveIntervalInSeconds;
  //
  //  private String keyPrefix = "keyPrefix";
  //
  //  public void setMaxInactiveIntervalInSeconds(Integer maxInactiveIntervalInSeconds) {
  //    this.maxInactiveIntervalInSeconds = maxInactiveIntervalInSeconds;
  //  }
  //
  //  public void setKeyPrefix(String keyPrefix) {
  //    this.keyPrefix = keyPrefix;
  //  }
  //
  //  @Bean
  //  public RedissonSessionRepository sessionRepository(
  //      RedissonClient redissonClient, ApplicationEventPublisher eventPublisher) {
  //    RedissonSessionRepository repository =
  //        new RedissonSessionRepository(redissonClient, eventPublisher);
  //    if (StringUtils.hasText(this.keyPrefix)) {
  //      repository.setKeyPrefix(this.keyPrefix);
  //    }
  //
  //    repository.setDefaultMaxInactiveInterval(this.maxInactiveIntervalInSeconds.intValue());
  //    return repository;
  //  }
  //
  //  public void setImportMetadata(AnnotationMetadata importMetadata) {
  //    Map<String, Object> map =
  //        importMetadata.getAnnotationAttributes(EnableRedissonHttpSession.class.getName());
  //    AnnotationAttributes attrs = AnnotationAttributes.fromMap(map);
  //    this.keyPrefix = attrs.getString("keyPrefix");
  //    this.maxInactiveIntervalInSeconds = (Integer)
  // attrs.getNumber("maxInactiveIntervalInSeconds");
  //  }

  // Cookie配置
  @Bean
  public CookieSerializer cookieSerializer() {
//    DynamicCookieMaxAgeCookieSerializer cookieSerializer = new DynamicCookieMaxAgeCookieSerializer();
    DefaultCookieSerializer cookieSerializer = new DefaultCookieSerializer();

    cookieSerializer.setCookieName(redisSessionCookieName); // sessionId名称
//    cookieSerializer.setSameSite("None");  // 设置SameSite属性

    return cookieSerializer;
  }

  // HttpSessionId配置
  @Bean
  public HttpSessionIdResolver httpSessionIdResolver() {
    CookieHttpSessionIdResolver cookieHttpSessionIdResolver = new CookieHttpSessionIdResolver();
    cookieHttpSessionIdResolver.setCookieSerializer(cookieSerializer());
    return cookieHttpSessionIdResolver;
  }
}
